package au.com.mason.propertyTracker.domain;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Transient;


@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Property
{
	private Integer id;
	private String streetAddress;
	private Suburb suburb;
	protected BigDecimal priceFrom;
	protected BigDecimal priceTo;
	private BigDecimal numberBedrooms;
	private BigDecimal numberBathrooms;
	private BigDecimal numberCarSpaces;
	private String comments;
	private Date dateListed;
	private String imageUrl;
	private String url;
	
	private String map;
	
	private String description;
	private Status status;
	private Date statusDate;
	
	private BigDecimal realEstatePropertyId;
	
	@Id
	@GeneratedValue
	@Column
	public Integer getId() {
		return id;
	}
	
	public void setId(final Integer id) {
		this.id = id;
	}

	@Column
	public String getStreetAddress() {
		return streetAddress;
	}

	public void setStreetAddress(String streetAddress) {
		this.streetAddress = streetAddress;
	}

	@OneToOne
    @JoinColumn(name="suburbId")
	public Suburb getSuburb() {
		return suburb;
	}

	public void setSuburb(Suburb suburb) {
		this.suburb = suburb;
	}

	@Column
	public BigDecimal getPriceFrom() {
		return priceFrom;
	}

	public void setPriceFrom(BigDecimal priceFrom) {
		this.priceFrom = priceFrom;
	}
	
	@Transient
	public String getPriceFromString() {
		if (priceFrom == null) return "";
		else {
			DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
			format.applyPattern("###,###,###.##");
			return format.format(priceFrom);
		}
	}	
	
	@Column
	public BigDecimal getPriceTo() {
		return priceTo;
	}
	
	@Transient
	public String getPriceToString() {
		if (priceTo == null) return "";
		else {
			DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
			format.applyPattern("###,###,###.##");
			return format.format(priceTo);
		}
	}

	public void setPriceTo(BigDecimal priceTo) {
		this.priceTo = priceTo;
	}	

	@Column
	public String getComments() {
		return comments;
	}
	
	@Transient
	public String getCommentsString() {
		String result = "";
		if (status != null) {
			SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy");

			String statusDateString = format.format(statusDate);

			if (isSold()) {
				result = "<b>SOLD " + statusDateString; 
			}
			else if (isRemoved()) {
				result = "<b>REMOVED " + statusDateString;
			}
			else if (isUnderContract()) {
				result = "<b>UNDER CONTRACT " + statusDateString;
			}
		}
		
		if (comments == null) return result;
		
		result += "<br>" + comments.replace("\r\n", "<br>").replace("'", "\\'");
		
		return result;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	@Column
	public BigDecimal getNumberBedrooms() {
		return numberBedrooms;
	}

	public void setNumberBedrooms(BigDecimal numberBedrooms) {
		this.numberBedrooms = numberBedrooms;
	}

	@Column
	public BigDecimal getNumberBathrooms() {
		return numberBathrooms;
	}

	public void setNumberBathrooms(BigDecimal numberBathrooms) {
		this.numberBathrooms = numberBathrooms;
	}

	@Column
	public BigDecimal getNumberCarSpaces() {
		return numberCarSpaces;
	}

	public void setNumberCarSpaces(BigDecimal numberCarSpaces) {
		this.numberCarSpaces = numberCarSpaces;
	}
	
	@Column
	public String getDescription() {
		return description;
	}
	
	@Transient
	public String getDescriptionString() {
		if (description == null) return "";
		String result = description.replace("\r\n", "<br>");
		result = result.replace("'", "\\'");
		
		return result;
	}	

	public void setDescription(String description) {
		this.description = description;
	}
	
	@Transient
	public String getNumberCarSpacesString() {
		if (numberCarSpaces == null) return "";
		else return numberCarSpaces.toString();
	}

	@Column
	public Date getDateListed() {
		return dateListed;
	}
	
	@Transient
	public String getDateListedString() {
		if (dateListed != null) {
			SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy");

			return format.format(dateListed);
		}
		else {
			return "";
		}
	}	

	public void setDateListed(Date dateListed) {
		this.dateListed = dateListed;
	}

	@Column
	public String getImageUrl() {
		if (imageUrl == null) return "";
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}


	
	@Transient
	public String getMapString() {
		if (map == null) return "";
		String result = map.replace((char)34 + "", "&quot;");
		return result;
	}
	
	@Column
	public String getMap() {
		return map;
	}

	public void setMap(String map) {
		this.map = map;
	}

	@Enumerated(EnumType.STRING)
	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
		this.statusDate = new Date();
	}

	@Column
	public Date getStatusDate() {
		return statusDate;
	}

	public void setStatusDate(Date statusDate) {
		this.statusDate = statusDate;
	}
	
	@Transient
	public boolean isUnderContract() {
		return status != null && status.equals(Status.UNDER_CONTRACT);
	}
	
	@Transient
	public boolean isSold() {
		return status != null && status.equals(Status.SOLD);
	}
	
	@Transient
	public boolean isRemoved() {
		return status != null && status.equals(Status.REMOVED);
	}

	@Column
	public BigDecimal getRealEstatePropertyId() {
		return realEstatePropertyId;
	}

	public void setRealEstatePropertyId(BigDecimal realEstatePropertyId) {
		this.realEstatePropertyId = realEstatePropertyId;
	}

	@Column
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
